﻿/// <reference path="vswd-ext_2.2.js" />

Ext.ns('PhotoList');
PhotoList.version = '1.0';

var g_PhotoWindowDictionary = new Dictionary();

//Ext.reg('phtolistgrid', PhotoList.Grid);


function PhotoListWindow(sid, devicename, data) {
    
    var _devicename = devicename ;
    var _data = data ;
    var _sid = sid;
    var _lastUploadImage = "";
    var _pagesize = 100;
    var _pageindex = 1;
    var _total;
    
    var btnShowMore = new Ext.Button({
        text: _Photo_ShowMore,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            showMore();
        }
    });

    var btnShowAll = new Ext.Button({
        text: _Photo_ShowAll,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            showAll();
        }
    });

    var btnSelectAll = new Ext.Button({
        text: _Photo_SelectAll,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sto.each(function(record) {
                record.set("choosed", true);
            });
        }
    });

    var btnSelectNone = new Ext.Button({
        text: _Photo_SelectNone,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sto.each(function(record) {
                record.set("choosed", false);
            });
        }
    });

    var btnGetSelected = new Ext.Button({
        text: _Photo_GetSelected,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            var mt88cmd = "BANCHGETIMAGE";
            var filenames = "";
            sto.each(function(record) {
                var choosed = record.get("choosed");
                if(choosed)
                    filenames += record.get("filename") + "|";
            });
            var mt88param = filenames;
            var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
            var gvvtparam = "";
            if (_sid != null && _sid != "" && filenames.length > 0)
                addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
        }
    });

    var btnDelSelected = new Ext.Button({
        text: _Photo_DeleteSelected,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            var mt88cmd = "BANCHDELETEIMAGE";
            var filenames = "";
            sto.each(function(record) {
                var choosed = record.get("choosed");
                if(choosed)
                    filenames += record.get("filename") + "|";
            });
            var mt88param = filenames;
            var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
            var gvvtparam = "";
            if (_sid != null && _sid != "" && filenames.length>0)
                addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
        }
    });

    function showMore() {
        var i;
        var mm = ((_total -1)< ((_pageindex + 1) * _pagesize)) ? (_total -1) : (_pageindex + 1)*_pagesize;
        for (i = _pageindex * _pagesize; i <mm; i++) {
            if (tmp[i].length > 0) {
                var d = new Object();
                d.choosed = false;
                d.filename = tmp[i];
                d.getphoto = 'icon-getimage';
                d.deletephoto = 'icon-delete';
                d.current = 0;
                d.total = 100;
                var record = new Ext.data.Record(d);
                sto.add(record);
            }
        }
        _pageindex++;
        grd.setTitle(_devicename + _Photo_Total+':' + tmp.length.toString() + _Photo_ShowTop+':' + (_total < _pageindex * _pagesize ? _total : _pageindex * _pagesize).toString());
    }

    function showAll() {
        var i;
        for (i = _pageindex * _pagesize; i < _total -1 ; i++) {
            if (tmp[i].length > 0) {
                var d = new Object();
                d.choosed = false;
                d.filename = tmp[i];
                d.getphoto = 'icon-getimage';
                d.deletephoto = 'icon-delete';
                d.current = 0;
                d.total = 100;
                var record = new Ext.data.Record(d);
                sto.add(record);
            }
        }
        _pageindex = (tmp.length % _pagesize == 0) ? (tmp.length / _pagesize) : (tmp.length / _pagesize + 1);
        grd.setTitle(_devicename + _Photo_Total+':' + tmp.length.toString() + _Photo_ShowTop+':' + (_total).toString());
    }
    
    function change(value, meta, rec, row, col, store) {    // add
        var id = Ext.id();
        (function() {
            new Ext.ProgressBar({
                renderTo: id,
                value: value / 100,
                text: value + '%'
            });
        }).defer(25)
        return '<span id="' + id + '"></span>';
    }


    var sto = new Ext.data.ArrayStore({
    fields: [{ name: 'choosed', type: 'bool' }, 'filename', { name: 'getphoto', type: 'string' }, { name: 'deletephoto', type: 'string' }, { name: 'current', type: 'int' }, { name: 'total', type: 'int'}],
        data: []
    });

    var tmp = _data.split('|');
    _total = tmp.length;
    var _index;
    var mm = (_total-1 < _pageindex * _pagesize) ? _total  : (_pageindex * _pagesize);
    for (_index = 0; _index < mm; _index++) {
        if (tmp[_index].length > 0) {
            var d = new Object();
            d.choosed = false;
            d.filename = tmp[_index];
            d.getphoto = 'icon-getimage';
            d.deletephoto = 'icon-delete';
            d.current = 0;
            d.total = 100;
            var record = new Ext.data.Record(d);
            sto.add(record);
        }
    }

    var chkColumnPhotoChoosed = new Ext.grid.CheckColumn({
        header: '',
        width: 25,
        sortable: false,
        dataIndex: 'choosed'
    });

    PhotoList.Grid = Ext.extend(Ext.grid.GridPanel, {
        initComponent: function() {

            // Create RowActions Plugin
            this.action = new Ext.ux.grid.RowActions({
                header: _TrackerMain_Actions
                //			,autoWidth:false
                //			,hideMode:'display'
		        , keepSelection: true
		        , actions: [{
		            iconCls: 'icon-getimage'
			        , tooltip: _Photo_GetPhoto
		        }, {
		            iconCls: 'icon-delete'
			            , tooltip: _Photo_DeletePhoto
                }]

		        , callbacks: {
		            'icon-getimage': function(grid, record, action, row, col) {
		                var filename = record.get('filename');
		                if (filename.length > 0) {
		                    var mt88cmd = EnumMT88Command.Get_Image_Data;
		                    var mt88param = filename + ",0";
		                    var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
		                    var gvvtparam = "";
		                    if (_sid != null && _sid != "")
		                        addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
		                }
		            }

		            , 'icon-delete': function(grid, record, action, row, col) {
		                var filename = record.get('filename');
		                if (filename.length > 0) {
		                    var mt88cmd = EnumMT88Command.Delete_Pictures;
		                    var mt88param = filename + "|";
		                    var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
		                    var gvvtparam = "";
		                    if (_sid != null && _sid != "")
		                        addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
		                }
		            }
		        }
            });

            //            // dummy action event handler - just outputs some arguments to console
            //            this.action.on({
            //                action: function(grid, record, action, row, col) {
            //                    Ext.ux.Toast.msg('Event: action', 'You have clicked row: <b>{0}</b>, action: <b>{1}</b>', row, action);
            //                }
            //            });

            // configure the grid
            Ext.apply(this, {
                //colModel: colPhotoListColumnModel
                columns: [
                     chkColumnPhotoChoosed,
                    { header: _Photo_PhotoName, width: 200, dataIndex: 'filename' },
                    new Ext.ux.ProgressColumn({
                        header: _Photo_Uploaded,
                        width: 105,
                        dataIndex: 'current',
                        divisor: 'total',
                        align: 'center',
                        renderer: function(value, meta, record, rowIndex, colIndex, store, pct) {
                            return Ext.util.Format.number(pct, "0.0%");
                        }
                    }),
                    this.action
                ]
	            , plugins: [this.action]
		        , loadMask: true
        	    , viewConfig: { forceFit: true }
            });
            PhotoList.Grid.superclass.initComponent.apply(this, arguments);
        }
	    , onRender: function() {

	        PhotoList.Grid.superclass.onRender.apply(this, arguments);
	    }
    });
    
    var grd = new PhotoList.Grid({
        store:sto,
        //autoHeight: true,
        //autoWidth: true,
        border: true,
        autoScroll: true,
        title: _devicename + _Photo_Total+':' + tmp.length.toString() + _Photo_ShowTop+':' + (_total < _pageindex * _pagesize ? _total : _pageindex * _pagesize).toString(),
        buttons: [btnShowMore, btnSelectAll, btnSelectNone, btnGetSelected, btnDelSelected]
    });
    
    grd.on('viewready', function(grid) {
        chkColumnPhotoChoosed.setMouseDownEvent(grid);
    });
//    var grd = new Ext.grid.GridPanel({
//        store: sto,
//        colModel: colPhotoListColumnModel,
//        plugins: [actiongetimage, actiondeleteimage, chkColumnPhotoChoosed],
//        width: 885,
//        height: 400,
//        title: _Photo_PhotoList,
//        buttons: [btnSelectAll, btnSelectNone, btnGetSelected, btnDelSelected]
//    });
    
    var win = new Ext.Window({
        title: _Photo_PhotoList,
        layout: 'fit',
        height: 470,
        width: 550,
        items: grd,
        iconCls:'report-photo',
        constrain:true,
        minimizable: true,
        listeners: {
        'minimize': function() {
                addTaskButtonForWindow(win);
            }
        },
        x: nextwindowx, //defined in paneldevicelist.js
        y: nextwindowy

    });

    win.show();
    nextwindowx += 20;
    nextwindowy += 20;

    this.show = function() {
        if (!win.isVisible())
            win.show();
    }

    this.close = function() {
        if (win != null)
            win.close();
    }

    this.hide = function() {
        if (win != null) {
            win.hide();
            if (g_PhotoWindowDictionary.ContainsKey(_sid)) {
                g_PhotoWindowDictionary.Remove(_sid);
            }
        }
    }

    this.destroy = function() {
        
    }

    this.changeImgUploadProgress = function(imgname, total, current) {
            if(imgname != _lastUploadImage){
                this.completeUploadImg();
                _lastUploadImage = imgname;
            }
            sto.each(function(record) {
                var tmpname = record.get("filename");

                if (tmpname == imgname) {
                    record.set("total", total);
                    record.set("current", current);
                }
            });
    };

    this.completeUploadImg = function() {
        sto.each(function(record) {
            var tmpimg = record.get("filename");
            if (tmpimg == _lastUploadImage) {
                record.set("total", 10);
                record.set("current", 10);
            }
        });
    };
    win.on('beforedestroy', function(obj) {
        if (g_PhotoWindowDictionary.ContainsKey(_sid)) {
            g_PhotoWindowDictionary.Remove(_sid);
        }
    })
    //return grd;
}